package service

import (
	"SecKill/common/define"
	"SecKill/common/helper"
	"SecKill/common/models"
	"SecKill/common/response"
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

func Hi(c *gin.Context) {
	response.ResponseOK(c)

}

//Session实现
//func SendCode(c *gin.Context) {
//	phone := c.Query("phone")
//	if !define.IsPhoneValid(phone) {
//		response.ResponseFailWithData(c, "手机号格式错误", "手机号格式错误", 0)
//	}
//	code := helper.GetRandNumber()
//	session := sessions.Default(c)
//	fmt.Println("code:", code)
//	session.Set("code:"+phone, code)
//	session.Options(sessions.Options{
//		MaxAge: 60 * 5,
//	})
//	if err := session.Save(); err != nil {
//		response.ResponseFailWithData(c, "发送验证码失败，请重试", "发送验证码失败，请重试", 0)
//	}
//	response.ResponseOK(c)
//}

// Redis实现
func SendCode(c *gin.Context) {
	phone := c.Query("phone")
	if !define.IsPhoneValid(phone) {
		response.ResponseFailWithData(c, "手机号格式错误", "手机号格式错误", 0)
	}
	code := helper.GetRandNumber()
	fmt.Println(define.LOGIN_CODE_KEY, code)
	models.RDb.SetEX(context.Background(), define.LOGIN_CODE_KEY+phone, code, define.LOGIN_CODE_TTL)
	response.ResponseOK(c)
}

// Session实现
//func Login(c *gin.Context) {
//	var loginFormDto models.LoginFormDto
//	if err := c.ShouldBindJSON(&loginFormDto); err != nil {
//		response.ResponseFail(c)
//		return
//	}
//	if !define.IsPhoneValid(loginFormDto.Phone) {
//		response.ResponseFailWithData(c, "手机号格式错误", "手机号格式错误", 0)
//	}
//	session := sessions.Default(c)
//	code := session.Get("code:" + loginFormDto.Phone)
//	if code == nil {
//		response.ResponseFailWithData(c, "验证码错误", "验证码错误", 0)
//	}
//	if loginFormDto.Code != code.(string) {
//		response.ResponseFailWithData(c, "验证码错误", "验证码错误", 0)
//	}
//	var count int64
//	models.Db.Model(new(models.User)).Where("phone = ?", loginFormDto.Phone).Count(&count)
//	var user models.User
//	if count > 0 {
//		models.Db.Model(new(models.User)).Where("phone = ?", loginFormDto.Phone).Find(&user)
//	} else {
//		user.Phone = loginFormDto.Phone
//		user.Password, _ = helper.GetBcryptPassword(loginFormDto.Password)
//		user.NickName = define.USER_NICK_NAME_PREFIX + helper.GetRandomStr(10)
//		if err := models.Db.Model(new(models.User)).Create(&user).Error; err != nil {
//			response.ResponseFailWithData(c, "注册失败", "注册失败", 0)
//		} else {
//			response.ResponseOKWithData(c, "注册成功", user, 0)
//			return
//		}
//	}
//	session.Set("user", user)
//
//	if err := session.Save(); err != nil {
//		response.ResponseFailWithData(c, "登录失败", "登录失败", 0) //todo
//		fmt.Println("err:", err)
//		return
//	}
//	sessionUser := session.Get("user")
//	fmt.Println("sessionUser:", sessionUser)
//	response.ResponseOK(c)
//	return
//
//}

func Login(c *gin.Context) {
	var loginFormDto models.LoginFormDto
	if err := c.ShouldBindJSON(&loginFormDto); err != nil {
		response.ResponseFail(c)
		return
	}
	if !define.IsPhoneValid(loginFormDto.Phone) {
		response.ResponseFailWithData(c, "手机号格式错误", "手机号格式错误", 0)
	}
	code, err := models.RDb.Get(context.Background(), define.LOGIN_CODE_KEY+loginFormDto.Phone).Result()
	if err != nil {
		response.ResponseFailWithData(c, "验证码错误", "验证码错误", 0)
	}

	if loginFormDto.Code != code {
		response.ResponseFailWithData(c, "验证码错误", "验证码错误", 0)
	}
	var count int64
	models.Db.Model(new(models.User)).Where("phone = ?", loginFormDto.Phone).Count(&count)
	var user models.User
	if count > 0 {
		models.Db.Model(new(models.User)).Where("phone = ?", loginFormDto.Phone).Find(&user)
	} else {
		user.Phone = loginFormDto.Phone
		user.Password, _ = helper.GetBcryptPassword(loginFormDto.Password)
		user.NickName = define.USER_NICK_NAME_PREFIX + helper.GetRandomStr(10)
		if err := models.Db.Model(new(models.User)).Create(&user).Error; err != nil {
			response.ResponseFailWithData(c, "注册失败", "注册失败", 0)
		} else {
			response.ResponseOKWithData(c, "注册成功", user, 0)
			return
		}
	}
	token := helper.GetUUID()
	userJson, _ := json.Marshal(user)
	models.RDb.Set(context.Background(), define.LOGIN_USER_KEY+token, userJson, define.LOGIN_USER_TTL)

	response.ResponseOKWithData(c, "登录成功", token, 0)
	return

}
func Me(c *gin.Context) {

	user, _ := c.Get("user")
	fmt.Println("user:", user)
	response.ResponseOKWithData(c, "获取用户信息成功", user, 0)
}
